var $ = require('../../common/helper.js');
Page({
    data: {
        id: '',
        name: '',
        mobile: '',
        idcard: '',
        social_number: '',
        gjj_number: '',
        idcard_pic1: '',
        idcard_pic2: '',
        type: '请选择户籍类型(必选)',
        type_text: '',
        idcard_front: '',
        idcard_back: ''
    },
    onLoad: function(options) {
        $.overload(false);
        var _this = this;
        var id = options.id;
        if (id) {
            $.setTitle('修改参保人');
            $.get('api.php?app=cart&act=person_edit&id=' + id, function(json) {
                _this.setData({
                    id: id,
                    name: json.data.name,
                    mobile: json.data.mobile,
                    idcard: json.data.idcard,
                    social_number: json.data.social_number,
                    gjj_number: json.data.gjj_number,
                    idcard_pic1: json.data.idcard_pic1,
                    idcard_pic2: json.data.idcard_pic2,
                    type: json.data.type,
                    type_text: 'text-x',
                    idcard_front: 'background-image:url(' + json.data.idcard_pic1 + ');background-size:cover;',
                    idcard_back: 'background-image:url(' + json.data.idcard_pic2 + ');background-size:cover;'
                });
            });
        } else {
            $.setTitle('新建参保人');
        }
    },
    selectImage: function(e) {
        var _this = this,
            type = e.currentTarget.dataset.type;
        $.selectImage(function(path) {
            $.upload('api.php?app=cart&act=idcard_pic', {
                name: 'idcard_pic',
                path: path
            }, function(json) {
                $.log(json)
                if (type == 'front') {
                    _this.setData({
                        idcard_pic1: json.data,
                        idcard_front: 'background-image:url(' + json.data + ');background-size:cover;'
                    });
                } else {
                    _this.setData({
                        idcard_pic2: json.data,
                        idcard_back: 'background-image:url(' + json.data + ');background-size:cover;'
                    });
                }
            });
        });
    },
    selectType: function(type) {
        this.setData({
            type: type,
            type_text: 'text-x'
        });
    },
    postForm: function(e) {
		var data = e.detail.value;
		if (!data.name.length) {
			$.overloadError('请输入真实姓名');
			return;
		}
		if (!data.mobile.length) {
			$.overloadError('请输入手机号码');
			return;
		}
		if (!$.isMobile(data.mobile)) {
			$.overloadError('手机号码错误');
			return;
		}
		if (!data.idcard.length) {
			$.overloadError('请输入身份证');
			return;
		}
		if (!$.isIdCard(data.idcard)) {
			$.overloadError('身份证错误');
			return;
		}
		if (!data.idcard_pic1.length || !data.idcard_pic2.length) {
			$.overloadError('请选择身份证照片');
			return;
		}
		if (!data.type.length) {
			$.overloadError('请选择户籍类型');
			return;
		}
        $.post('api.php?app=cart&act=person_edit', data, function(json) {
            wx.navigateBack();
        });
    }
})